package com.leyou.item.controller;

import com.leyou.item.service.SpecService;
import com.leyou.pojo.SpecGroup;
import com.leyou.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XiaoLuo
 * @ClassName: SpecController
 * @Description: ${todo}
 * @date 2019/4/17 17:23
 */
@RestController @RequestMapping("spec") public class SpecController {
	@Autowired private SpecService specServiceImpl;

	/**
	 * 根据商品分类ID查询分类对应的规格组
	 * @param cid
	 * @return
	 */
	@GetMapping("groups/{cid}") public ResponseEntity<List<SpecGroup>> querySpecGroups(
			@PathVariable("cid") Long cid) {
		List<SpecGroup> specGroups = this.specServiceImpl.querySpecGroups(cid);
			if (specGroups != null && specGroups.size() != 0) {
				return ResponseEntity.ok(specGroups);
			}
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();//204
		}

	/**
	 * 根据商品规则组的id查询对应的规格参数
	 *
	 * 或者根据商品分类的ID查询对应的规格参数
	 * @param gid
	 * @return
	 */
		@GetMapping("params")
	public ResponseEntity<List<SpecParam>> querySpecParam(
			@RequestParam(value = "gid" , required = false) Long gid,
			@RequestParam(value = "cid" , required = false) Long cid,
			@RequestParam(value = "searching" , required = false) Boolean searching,
			@RequestParam(value = "generic" , required = false) Boolean generic
		){
			List<SpecParam> specParams = this.specServiceImpl.querySpecParam(gid,cid,searching,generic);
			if(specParams!=null && specParams.size()!=0){
				return ResponseEntity.ok(specParams);
			}
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();//204
		}
}


